import random

WIDTH = 300
HEIGHT = 300

BACK_COLOR = (0, 0, 0)

class MyRect:
    def __init__(self, x, y, w, h, clr):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.clr = clr
        self.speed = 2
    def SetSpeed(self, speed):
        self.speed = speed

    def Update(self):
        self.x += self.speed
        if (self.speed > 0 and self.x + self.w // 2 > WIDTH) or (self.speed < 0 and self.x - self.w // 2 < 0):
            self.speed = -self.speed
            self.speed += self.speed * (random.random()- 0.5)

        if self.x + self.w // 2 > WIDTH:
            self.x = WIDTH - self.w // 2
        elif self.x - self.w // 2 < 0:
            self.x = self.w // 2

    def Draw(self):
        rc = Rect((self.x - self.w // 2, self.y - self.h // 2), (self.w, self.h))
        screen.draw.filled_rect(rc, self.clr)

clr_list = [
    (0, 200, 0),
    (200, 0, 0),
    (0, 0, 200),
    (0, 200, 200),
    (200, 0, 200),
    (200, 200, 0),
    (200, 200, 200),
]
clr_table=[
[0,1,2,3,4,5],
[5,0,1,2,3,4],
[4,5,0,1,2,3],
[3,4,5,0,1,2],
[2,3,4,5,0,1],
[1,2,3,4,5,0],
]
rc_list = []

def ct():
    global clr_table
    for i in range(0,8):
        clr_table.append([])
        for j in range(0,8):
            clr_table[i].append(-1)
    start=random.randint(0,6)
    for i in range(1,8):
        for j in range(1,8):
            clr_l=0
            for k in range(0,6):
                if (k==clr_table[i-1][j]) or (k==clr_table[i][j-1]):
                    continue
                else:
                    if(k==6 or clr_l<2):
                        clr_l=k
                        break
            clr_table[i][j]=clr_l

def SET():
    ct()
    for i in range(0, 301,50):
        for j in range(0, 301,50):
            clr_index=clr_table[int(i/50)+1][int(j/50)+1]
            #            clr_index=random.randint(0,6)
            clr=clr_list[clr_index]
            rc = MyRect(i+25,j+25,50,50, clr)
            rc.SetSpeed(random.randint(1, 10))
            rc_list.append(rc)

def on_key_down(key):
    if key == keys.ESCAPE:
        quit()
    if key==keys.A:
        print("again")
        SET()
def on_mouse_down(pos):
    print("mouse_down", pos)
def on_mouse_up(pos):
    print("mouse_up", pos)

def update():
    pass

def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    for rc in rc_list:
        rc.Draw()
SET()